﻿Imports System.Windows.Forms
Imports System.IO

Public Class StoreProperties
    Public StoreName As String
    Public StorePath As String
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
    Sub InitializeAvailableProperties()
        Try
            For Each item In Directory.GetFiles(StorePath, "*.rar")
                Dim FileInfo_ As New FileInfo(item)
                Dim row As String() = New String() {FileInfo_.Name, FileInfo_.CreationTime}
                DataGRidAvailable.Rows.Add(row)
            Next
            InitializeAppliedProperties()
        Catch ex As Exception
        End Try
        DeFreezeTab1()
    End Sub
    Sub InitializeAppliedProperties()
        Try
            For Each folder In Directory.GetDirectories(StorePath)
                Dim DirectoryInfo_ As New DirectoryInfo(folder)
                Dim title As String = ""
                Dim DateApplied As String = ""
                Dim Status As String = ""
                Dim Method As String = ""
                For Each Item In Directory.GetFiles(DirectoryInfo_.FullName)
                    Dim FileItem As New FileInfo(Item)
                    If FileItem.Name = "Title.txt" Then
                        title = ReadTextFile(FileItem.FullName)
                    ElseIf FileItem.Name = "DateApplied.txt" Then
                        DateApplied = ReadTextFile(FileItem.FullName)
                    ElseIf FileItem.Name = "Status.txt" Then
                        Status = ReadTextFile(FileItem.FullName)
                    ElseIf FileItem.Name = "Method.txt" Then
                        Method = ReadTextFile(FileItem.FullName)
                    End If
                Next

                If title <> "" Then
                    Dim row As String() = New String() {title, DateApplied, Status, Method}
                    DataGridApplied.Rows.Add(row)
                End If

                DataGridApplied.Sort(DataGridApplied.Columns("DateApplied_"), System.ComponentModel.ListSortDirection.Ascending)
            Next
        Catch ex As Exception

        End Try
    End Sub

    Sub FreezeTab1()
        For Each contrl As Control In TabPage1.Controls
            Try
                contrl.Enabled = False
            Catch ex As Exception
            End Try
        Next
    End Sub
    Sub DeFreezeTab1()
        For Each contrl As Control In TabPage1.Controls
            Try
                contrl.Enabled = True
            Catch ex As Exception
            End Try
        Next
    End Sub
    Sub FreezeTab2()
        For Each contrl As Control In TabPage2.Controls
            Try
                contrl.Enabled = False
            Catch ex As Exception
            End Try
        Next
    End Sub
    Sub DeFreezeTab2()
        For Each contrl As Control In TabPage2.Controls
            Try
                contrl.Enabled = True
            Catch ex As Exception
            End Try
        Next
    End Sub
    Function ReadTextFile(TextFile As String) As String
        Dim TextLine As String = ""
        Try
            If System.IO.File.Exists(TextFile) = True Then
                Dim objReader As New System.IO.StreamReader(TextFile)
                Do While objReader.Peek() <> -1
                    TextLine = TextLine & objReader.ReadLine() & vbNewLine
                Loop
                objReader.Close()
                Return TextLine.Trim
            Else
                Return Nothing
            End If

        Catch ex As Exception

        End Try
        Return Nothing
    End Function
    Private Sub StoreProperties_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Text = StoreName & " - Properties"
        CheckForIllegalCrossThreadCalls = False
        FreezeTab1()
        FreezeTab2()

        Dim t As New Threading.Thread(AddressOf InitializeAvailableProperties)
        t.Start()

        Dim r As New Threading.Thread(AddressOf InitializeGeneralProp)
        r.Start()


    End Sub

    Private Sub InitializeGeneralProp()
        Try
            If Directory.Exists(StorePath) Then
                Dim DirectoryInfo_ As New DirectoryInfo(StorePath)
                txtlocation.Text = DirectoryInfo_.Name
                lblcreated.Text = DirectoryInfo_.CreationTime
                lblcontains.Text = Directory.GetFiles(DirectoryInfo_.FullName).Count & " Files and " & Directory.GetDirectories(DirectoryInfo_.FullName).Count & " Folders"
                If File.Exists(StorePath & "AutoRollOut.exe") Then
                    Dim StoreVersion As Version = GetFileVersionInfo(StorePath & "AutoRollOut.exe")
                    lblversion.Text = StoreVersion.ToString
                Else
                    lblversion.Text = "Unavailable"
                End If
            Else
                lblcreated.Text = "Unavailable"
                lblcontains.Text = "Unavailable"
                lblversion.Text = "Unavailable"
            End If
        Catch ex As Exception

        End Try
        DeFreezeTab2()
    End Sub
    Private Function GetFileVersionInfo(ByVal filename As String) As Version
        Return Version.Parse(FileVersionInfo.GetVersionInfo(filename).FileVersion)
    End Function
End Class
